package person.bing.fbcloud.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: mengfanbing
 * @date: 2020/10/14 10:27
 */
public class Q1002 {
    public static void main(String[] args) {
        String[] A = {"bella","label","roller"};
        List<String> stringList = commonChars(A);
        System.out.println(stringList);
    }
    public static List<String> commonChars(String[] A) {
        List<String> ans = new ArrayList<>();
        if (A==null || A.length==0){
            return ans;
        }
        int length = A.length;
        List<Character> temp = new ArrayList<>();
        List<Character> tempAns = A[0].chars().mapToObj(i -> (char) i)
                .collect(Collectors.toList());
        for (int i=1;i<length;i++){
            List<Character> iChars = A[i].chars().mapToObj(a -> (char) a)
                    .collect(Collectors.toList());
            for (int j=0;j<tempAns.size();j++){
                if(iChars.contains(tempAns.get(j))){
                    temp.add(tempAns.get(j));
                    int index = iChars.indexOf(tempAns.get(j));
                    iChars.remove(index);
                }
            }
            tempAns = temp;
            temp = new ArrayList<>();
        }
        for (Character i:tempAns){
            ans.add(String.valueOf(i));
        }
        return ans;
    }
}
